import { useTitle } from 'ahooks'
import { Button, Form, Input, NumberKeyboard, Switch, Toast } from 'antd-mobile'
import React, { useEffect } from 'react'
import { history } from 'umi';
import './Components/TitleAddOrUpdate.less'
import style from './Components/TitleAddOrUpdate.less'
import { addCollectTickets, deleteCollectTicket, upDataCollectTickets } from '@/services/Title';


export default function TitleAddOrUpdate() {
    
    const addOrUpdate = history.location.state; 
    // console.log(addOrUpdate);
    
    addOrUpdate ?  useTitle("编辑抬头") : useTitle("新增抬头")
    const onFinish = async(data: any) => {
        // console.log(data);
        const value:API.CollectTicketsList ={
            userId:1,
            enterprise:data?.enterprise??undefined,
            taxId:data?.taxId??undefined,
            account: data?.account??undefined,
            accountBank: data?.accountBank??undefined,
            address: data?.address??undefined,
            bankBranches: data?.bankBranches??undefined,
            defaultStatus: data.isDefault===undefined?'false':String(data.isDefault),
            phone: data.phone??undefined,
            id:addOrUpdate?.data?.id??undefined,
        }

        const value2:API.CollectTicketsList ={
            userId:1,
            enterprise:data?.enterprise??undefined,
            taxId:data?.taxId??undefined,
            account: data?.account??undefined,
            accountBank: data?.accountBank??undefined,
            address: data?.address??undefined,
            bankBranches: data?.bankBranches??undefined,
            defaultStatus: data.isDefault===undefined?'false':String(data.isDefault),
            phone: data.phone??undefined,
        }

        if(addOrUpdate){
            //修改
            console.log(value);
            const res = await upDataCollectTickets(value)
            // console.log(res);
            if(res.success){
                // history.push(
                //     '/Invoice/InvoiceTitle'
                // )
                Toast.show({content:res.message})
                history.go(-1)
            }
            
        }else{
            //新增
            
            const res = await addCollectTickets(value2);
            console.log(res);
            if(res.success){
                Toast.show({content:res.message})
                history.go(-1)
            }
        }   
    }
    const handleDel=async(id:string)=>{
        const res = await deleteCollectTicket(id)
        // console.log(res,id);
        if(res.success){
            Toast.show({content:res.message})
            history.go(-1)
        }
        
    }
    const [form] = Form.useForm();
    useEffect(() => {
        if (addOrUpdate) {

            form.setFieldsValue(addOrUpdate?.data)
            
            
        } else {

        }
    }, [])
    return (

        <div className='styles'>
            <Form
                form={form}
                onFinish={onFinish}
                layout='horizontal'
                footer={
                    <div className='style_button'>
                        {!addOrUpdate ?
                            <Button color="primary" block type='submit' size='large'
                                style={{ fontSize: "15px", backgroundColor: '#21B47C', "margin": "6px 20px 6px 20px" }}>保存</Button>
                            :
                            <div className='style_div_submit' >
                                <Button block onClick={()=>handleDel(addOrUpdate?.data?.id)} style={{ fontSize: "15px", width: "162.5px", border: "1px solid #21B47C", color: "#21B47C" }}  size='large'>删除抬头</Button>
                                <Button type='submit' block style={{ fontSize: "15px", width: "162.5px", backgroundColor: '#21B47C', color: "#FFFFFF" }} type='submit' size='large' >保存修改</Button>
                            </div>
                        }
                    </div>
                }
            >
                <div className='style_div1'>
                    <div className='style_div1_span'>发票抬头</div>
                    <div className={style.div2}>
                        <Form.Item
                            name="enterprise"
                            label="收票企业"
                            style={{ fontSize: "15px" }}
                        >
                            <Input style={{ '--font-size': "15px", '--text-align': 'right' }} clearable placeholder='请输入' />
                        </Form.Item>
                        <Form.Item
                            name="taxId"
                            label="纳税人识别号"
                            style={{ fontSize: "15px" }}
                        >
                            <Input style={{ '--font-size': "15px", '--text-align': 'right' }} clearable placeholder='请输入' />
                        </Form.Item>
                        <Form.Item
                            name="address"
                            label="地址"
                            style={{ fontSize: "15px" }}
                        >
                            <Input style={{ '--font-size': "15px", '--text-align': 'right' }} clearable placeholder='非必填' />
                        </Form.Item>
                        <Form.Item
                            name="phone"
                            label="电话"
                            style={{ fontSize: "15px" }}
                        >
                            <Input style={{ '--font-size': "15px", '--text-align': 'right' }} clearable placeholder='非必填' />
                        </Form.Item>
                        <Form.Item
                            name="accountBank"
                            label="开户行"
                            style={{ fontSize: "15px" }}
                        >
                            <Input style={{ '--font-size': "15px", '--text-align': 'right' }} clearable placeholder='非必填' />
                        </Form.Item>
                        <Form.Item
                            name="bankBranches"
                            label="银行网点"
                            style={{ fontSize: "15px" }}
                        >
                            <Input style={{ '--font-size': "15px", '--text-align': 'right' }} clearable placeholder='非必填' />
                        </Form.Item>
                        <Form.Item
                            name="account"
                            label="开户账号"
                            style={{ fontSize: "15px" }}
                        >
                            <Input style={{ '--font-size': "15px", '--text-align': 'right' }} clearable placeholder='非必填' />
                        </Form.Item>
                    </div>
                </div>
                <div className='style_div2' >
                    <Form.Item
                        name="isDefault"
                        label={<span style={{ color: "rgba(51, 51, 51, 1)" }}>设置为默认值</span>} >
                        <Switch {...addOrUpdate?.data?.defaultStatus==true?defaultChecked:''} style={{ float: "right", "--checked-color": "rgba(33, 180, 124, 1)" }} />
                    </Form.Item>
                </div>
            </Form>
        </div>

    )
}
